/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.logistics.web.updatecarframenumber;

import com.alibaba.fastjson.JSONArray;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.custom.enumInfo.tableNameEnum;
import com.jeesite.modules.logistics.service.updatecarframenumber.UpdateFrameNumberService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 车架号修改Controller
 *
 * @author LV
 * @version 2021-03-05
 */
@Controller
@RequestMapping(value = "${adminPath}/logistics/updatecarframenumber")
public class UpdateFrameNumberController extends BaseController {

    @Autowired
    private UpdateFrameNumberService updateFrameNumberService;

    /**
     * 查询列表
     */
    @RequiresPermissions("logistics:updatecarframenumber:view")
    @RequestMapping(value = {"updaleList", ""})
    public String updaleList(ServiceOrder serviceOrder, Model model) {
        model.addAttribute("serviceOrder", serviceOrder);
        return "modules/logistics/updatecarframenumber/updateCarFrameNumber";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("logistics:updatecarframenumber:view")
    @RequestMapping(value = "updateCarFrameNumberListData")
    @ResponseBody
    public Page<ServiceOrder> updateCarFrameNumberListData(ServiceOrder serviceOrder, HttpServletRequest request, HttpServletResponse response) {
        serviceOrder.setPage(new Page<>(request, response));
        Page<ServiceOrder> page = updateFrameNumberService.updateCarFrameNumber(serviceOrder);
        return page;
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("logistics:updatecarframenumber:edit")
    @PostMapping(value = "updateFrameNumber")
    @ResponseBody
    @Transactional(readOnly = false)
    public String updateFrameNumber(@RequestParam("frameNumberArr") String jsonStr) {
        List<Obj> objList = JSONArray.parseArray(jsonStr, Obj.class);
        for (Obj obj : objList) {
            for (tableNameEnum tableNameEnum : tableNameEnum.values()) {
                if (StringUtils.isNotEmpty(obj.getNewName())) {
                    updateFrameNumberService.updateFrameNumber(tableNameEnum.getCode(), obj.getOldName(), obj.getNewName());
                } else {
                    return renderResult(Global.FALSE, text("修改的车架号存在空值！"));
                }
            }
        }
        return renderResult(Global.TRUE, text("修改车架号成功！"));
    }

    public static class Obj {
        private String oldName;
        private String newName;


        public String getOldName() {
            return oldName;
        }

        public void setOldName(String oldName) {
            this.oldName = oldName;
        }

        public String getNewName() {
            return newName;
        }

        public void setNewName(String newName) {
            this.newName = newName;
        }
    }

}